﻿<%@ Page Language="C#" MasterPageFile="~/Manager.master" AutoEventWireup="true" CodeFile="BasketApp.aspx.cs"
    Inherits="Manager_OrderApp_BasketApp" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

    <script language="javascript" src="../../Pub/Js/jquery.js" type="text/javascript"></script>

    <div>
        &nbsp;<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
            <tr>
                <td>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <input id="hidUSD" type="hidden" runat="server" />
                    <asp:Label ID="lblUSD" runat="server"></asp:Label></td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="grv_Master" runat="server" AutoGenerateColumns="False" Width="100%"
                        CellPadding="3" BorderWidth="1px" OnRowDeleting="grv_Master_RowDeleting" OnRowDataBound="grv_Master_RowDataBound"
                        BorderColor="#407db3" DataKeyNames="PDID">
                        <Columns>
                            <asp:TemplateField HeaderText="X&#243;a">
                                <ItemTemplate>
                                   <%--  <asp:LinkButton ID="lblDelete" runat="server" CommandName="delete" Text="<img src=../../Images/Icon/cmdDelete.gif border=0 alt=Delete > "></asp:LinkButton>--%>
                                   <img src='../../Images/Icon/cmdDelete.gif' border='0' alt='Delete' onclick="fnDeleteRow($(this))" style="cursor:pointer">
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Center" />
                                <HeaderStyle CssClass="cssBorderGrv" HorizontalAlign="Center" VerticalAlign="Middle"
                                    Wrap="False" Width="1%" />
                                <FooterStyle CssClass="cssBorderGrv" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="M&#227; sản phẩm">
                                <ItemTemplate>
                                    <asp:Label ID="lblMaSP" runat="server" Text='<%# Eval("PDID") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Center" />
                                <HeaderStyle CssClass="cssBorderGrv" HorizontalAlign="Center" VerticalAlign="Middle"
                                    Wrap="False" Width="1%" />
                                <FooterStyle CssClass="cssBorderGrv" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="T&#234;n sản phẩm">
                                <ItemTemplate>
                                    <asp:Label ID="lblTenSP" runat="server" Text='<%# Eval("PDName") %>' Width="40%"></asp:Label>
                                     <input id="hidKindNo" type="hidden" runat="server" value='<%# Eval("KindNo") %>' /> 
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Left" />
                                <HeaderStyle CssClass="cssBorderGrv" HorizontalAlign="Left" VerticalAlign="Middle"
                                    Wrap="False" />
                                <FooterStyle CssClass="cssBorderGrv" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Số lượng">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtSoLuong" runat="server" Width="40px" CssClass="txt_Product" Height="17px"
                                        onkeyup="fnUpdateQuantity(this)" Style="vertical-align: middle; text-align: center"
                                        MaxLength="2" Text='<%# Eval("Quantity") %>'></asp:TextBox>
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Center" Width="1%" />
                                <HeaderStyle CssClass="cssBorderGrv" HorizontalAlign="Center" VerticalAlign="Middle"
                                    Wrap="False" />
                                <FooterStyle CssClass="cssBorderGrv" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Giá">
                                <ItemTemplate>
                                    <asp:Label ID="lblDonGia" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
                                    <asp:Label ID="lblRate" runat="server" Text='<%# Eval("Rate") %>'></asp:Label>
                                  
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Center" Wrap="false" />
                                <HeaderStyle CssClass="cssBorderGrv" HorizontalAlign="Center" VerticalAlign="Middle"
                                    Wrap="False" Width="10%" />
                                <FooterStyle CssClass="cssBorderGrv" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText='Giá mới'>
                                <ItemStyle HorizontalAlign="Center" Width="2%" />
                                <ItemTemplate>
                                    <asp:TextBox ID="txtSaleOfPrice" runat="server" Width="100%" CssClass="txt_Product"
                                        Height="18px" Text='<%# Eval("DiscountPrice") %>' Style="text-align: right; width: 120px"
                                        onkeyup="fnCalSaleOf(this)"></asp:TextBox>
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" BorderWidth="1px" HorizontalAlign="Center" BorderStyle="Solid"
                                    BorderColor="#407db3" Wrap="false" Width="10%" />
                                <HeaderStyle CssClass="cssBorderGrv" BorderWidth="1px" BorderStyle="Solid" BorderColor="#407db3" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Thanh toán (VNĐ)">
                                <ItemStyle HorizontalAlign="Center" Width="2%" />
                                <ItemTemplate>
                                    <asp:TextBox ID="lblTotal" runat="server" Text='<%# Eval("Total") %>' Width="100%"
                                        onfocus="fnReadOnlyText(this)" Style="text-align: right; border: 0; background-color: Transparent;
                                        width: 120px"></asp:TextBox>
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" BorderWidth="1px" HorizontalAlign="Center" BorderStyle="Solid"
                                    BorderColor="#407db3" Wrap="false" />
                                <HeaderStyle CssClass="cssBorderGrv" BorderWidth="1px" BorderStyle="Solid" BorderColor="#407db3"
                                    Wrap="false" />
                            </asp:TemplateField>                            
                            <asp:TemplateField HeaderText="Ghi chú">
                                <ItemStyle HorizontalAlign="Center" Width="2%" />
                                <ItemTemplate>
                                    <asp:TextBox ID="txtNoteDetail" runat="server" Width="150px" Font-Size="11px" Text='<%# Eval("NoteDetail") %>'></asp:TextBox>
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" BorderWidth="1px" HorizontalAlign="Center" BorderStyle="Solid"
                                    BorderColor="#407db3" Wrap="false" />
                                <HeaderStyle CssClass="cssBorderGrv" Wrap="false" BorderWidth="1px" BorderStyle="Solid"
                                    BorderColor="#407db3" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Kho">
                                <ItemTemplate>
                                    <asp:Label ID="lblSL" runat="server" Style="vertical-align: middle; text-align: center"
                                        Text=''></asp:Label>
                                </ItemTemplate>
                                <ItemStyle CssClass="cssBorderGrv" HorizontalAlign="Center" Width="1%" />
                                <HeaderStyle CssClass="cssBorderGrv" HorizontalAlign="Center" VerticalAlign="Middle"
                                    Wrap="False" />
                                <FooterStyle CssClass="cssBorderGrv" />
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <div align="center">
                                <asp:Label ID="Label1" runat="server" Text="Chưa có sản phẩm nào trong giỏ hàng của bạn"
                                    CssClass="lbl_TextDefault"></asp:Label>
                            </div>
                        </EmptyDataTemplate>
                        <RowStyle CssClass="gvRowStyle" />
                        <AlternatingRowStyle CssClass="gvAlternatingRowStyle" />
                        <HeaderStyle CssClass="gvHeader" VerticalAlign="Middle" Wrap="False" />
                       <FooterStyle CssClass="gv_FooterBasket" BackColor="paleturquoise" />
                    </asp:GridView>
                    <br />
                </td>
            </tr>
            <tr>
                <td align="center">
                    <table border="0" bordercolor="#407db3" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
                        <tr>
                            <td align="center" colspan="6" style="white-space: nowrap; background-color: honeydew;" valign="bottom">
                                <asp:CheckBox ID="chkPayment" runat="server" Style="cursor: pointer"
                                    Text="Đã thanh toán đầy đủ" Font-Bold="True" /><br />
                                <table id="tblPaymoney" runat="server" cellpadding="3" cellspacing="0">
                                    <tr>
                                        <td>
                                            Số tiền trả:
                                        </td>
                                        <td>
                                            <asp:TextBox ID="txtPaidMoney" runat="server" CssClass="txt_Product" onkeyup="fnDisplayTextMoney(this)"
                                                Style="text-align: right" Text="0" Width="120px"></asp:TextBox>
                                        </td>
                                        <td>
                                            <asp:TextBox ID="TextBox2" runat="server" BackColor="TRansparent" BorderStyle="None"
                                                CssClass="txt_Product" Font-Size="11px" ReadOnly="true" Style="text-align: right"
                                                Text="VNĐ" Width="28px"></asp:TextBox>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td align="right" style="padding-right: 10px; white-space: nowrap" valign="bottom">
                                <asp:Label ID="Label14" runat="server" Font-Bold="True" Text="Tổng số lượng: "></asp:Label></td>
                            <td align="right" colspan="2" style="width: 120px; border-bottom: black thin dotted;
                                white-space: nowrap" valign="bottom">
                                <asp:Label ID="lblQuantity" runat="server" Font-Names="tahoma" Font-Size="12px"></asp:Label></td>
                            <td align="right" style="width: 2%; white-space: nowrap">
                            </td>
                            <td id="td1" runat="server" align="right" style="padding-right: 10px; white-space: nowrap"
                                valign="bottom">
                                <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Số tiền đã trả:"></asp:Label></td>
                            <td id="td2" runat="server" align="right" style="border-bottom: black thin dotted;
                                white-space: nowrap" valign="bottom">
                                <asp:TextBox ID="txtRepaid" runat="server" CssClass="txt_Product" Height="18px" onfocus="fnReadOnlyText(this)"
                                    Style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px;
                                    text-align: right" Width="120px"></asp:TextBox>
                                <asp:TextBox ID="txt" runat="server" BorderStyle="None" CssClass="txt_Product" Font-Size="11px"
                                    ReadOnly="true" Style="text-align: right" Text="VNĐ" Width="28px"></asp:TextBox></td>
                        </tr>
                        <tr>
                            <td align="right" style="padding-right: 10px; white-space: nowrap" valign="bottom">
                                <asp:Label ID="Label15" runat="server" Font-Bold="True" Text="Tổng số tiền thanh toán: "></asp:Label></td>
                            <td align="right" colspan="2" style="border-bottom: black thin dotted; white-space: nowrap"
                                valign="bottom">
                                <asp:TextBox ID="lblTongsotien" runat="server" CssClass="txt_Product" Height="18px"
                                    onfocus="fnReadOnlyText(this)" Style="border-right: 0px; border-top: 0px; border-left: 0px;
                                    border-bottom: 0px; text-align: right" Width="120px"></asp:TextBox>
                                <asp:TextBox ID="TextBox4" runat="server" BorderStyle="None" CssClass="txt_Product"
                                    Font-Size="11px" ReadOnly="true" Style="text-align: right" Text="VNĐ" Width="28px"></asp:TextBox></td>
                            <td align="right" style="width: 2%; white-space: nowrap">
                            </td>
                            <td id="td3" runat="server" align="right" style="padding-right: 10px; white-space: nowrap"
                                valign="bottom">
                                <asp:Label ID="Label3" runat="server" Font-Bold="True" Text="Số tiền còn thiếu lại:"></asp:Label></td>
                            <td id="td4" runat="server" align="right" style="border-bottom: black thin dotted;
                                white-space: nowrap" valign="bottom">
                                <asp:TextBox ID="txtRemainPrice" runat="server" CssClass="txt_Product" Height="18px"
                                    onfocus="fnReadOnlyText(this)" Style="border-right: 0px; border-top: 0px; border-left: 0px;
                                    border-bottom: 0px; text-align: right" Width="120px"></asp:TextBox>
                                <asp:TextBox ID="TextBox1" runat="server" BorderStyle="None" CssClass="txt_Product"
                                    Font-Size="11px" ReadOnly="true" Style="text-align: right" Text="VNĐ" Width="28px"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                    &nbsp;</td>
            </tr>
            <tr>
                <td align="center">
                    <table style="width: 100%" id="tdNote" cellspacing="0" cellpadding="0" border="0"
                        runat="server">
                        <tbody>
                            <tr>
                                <td style="padding-left: 10px; width: 10%" align="center">
                                    <asp:Label Style="padding-left: 5%" ID="lblNoteOrder" runat="server" Text="Ghi chú"></asp:Label></td>
                                <td>
                                    <asp:TextBox ID="txtNote" runat="server" CssClass="txt_Product" Width="95%" BorderStyle="Groove"
                                        Height="67px" TextMode="MultiLine"></asp:TextBox>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <asp:Button ID="btnBack" runat="server" CssClass="cssButton" Text="Tạo đơn mới" OnClick="btnBack_Click"
                        OnClientClick="if(confirm('Bạn muốn hủy bỏ đơn hàng này???')) return true; return false;" />&nbsp;
                    <asp:Button ID="btnOrder" runat="server" CssClass="cssButton" Text="Đặt hàng" OnClick="btnOrder_Click" Width="105px" />&nbsp;
                </td>
            </tr>
            <tr>
                <td align="center">
                    &nbsp;</td>
            </tr>
            <tr>
                <td align="center">
                    </td>
            </tr>
        </table>
    </div>
    <input type="text" readonly="readonly" id="txtReadOnly" runat="server" style="text-align: right;
        border: 0" />
    <input type="hidden" runat="server" id="hidRemainPrice" />
    <input type="hidden" runat="server" id="hidRepaid" />
    <input type="hidden" runat="server" id="hidOrderDelete" />

    <script language="javascript" type="text/javascript">
     function fnReadOnlyText(obj)
    {
        document.getElementById("<%= txtReadOnly.ClientID %>").select();
        document.getElementById("<%= txtReadOnly.ClientID %>").focus();   
        return false;
            
    }
   
    
   /* 
    function fnCalSaleOf(obj)
    {       
    
           obj.value=FormatMoney( obj.value.ReplaceAll(",",""))   ;         
           var Quantity=obj.parentNode.parentNode.cells[3].children[0].value;
           var Rate=obj.parentNode.parentNode.cells[4].children[1].innerHTML;             
           var DiscountPrice=obj.value.ReplaceAll(",","");

          //neu = 0 lay lai gia goc 
           if(obj.value=="" || obj.value=="0")
           {   
               DiscountPrice=obj.parentNode.parentNode.cells[4].children[0].innerHTML.ReplaceAll(",","");               
           }          
       
           var objTotalPrice=obj.parentNode.parentNode.cells[8].children[0];
           var TotalPrice= DiscountPrice*Quantity;  
                      
           if (Rate=="USD")
           { 
               var USD=document.getElementById("<%= hidUSD.ClientID %>").value ;      
               TotalPrice*= USD;         
           }

            objTotalPrice.value=FormatMoney(TotalPrice);
         
          //Prepaid 
          var objPrepaid=obj.parentNode.parentNode.cells[6].children[0];
          if(objPrepaid.value!="" || objPrepaid.value!="0" ) 
          {                 
                objPrepaid.value="0" ;
                fnCalPrepiad(objPrepaid);
                return;                  
           }  
    }
    
    function fnCalPrepiad(obj)
    {        
       obj.value=FormatMoney( obj.value.ReplaceAll(",",""))      
       var Prepaid=obj.value.ReplaceAll(",","");
       if(Prepaid!="" || Prepaid!="0" ) 
       {    
            var TotalPrice ;             
            TotalPrice=obj.parentNode.parentNode.cells[8].children[0].value.ReplaceAll(",","");               
  
            var RemailPrice=FormatMoney(TotalPrice-Prepaid);
            if(TotalPrice-Prepaid<0)
            {
                alert('Số tiền nhập vào đã nhập vượt mức số tiền tiền thanh toán');
                obj.value="0" ;
                fnCalPrepiad(obj);
                return; 
            } 
            obj.parentNode.parentNode.cells[7].children[0].value=RemailPrice;    
          
        }        
    }
    */
    function fnUpdateQuantity(obj)
    {
         obj.value=FormatMoney(obj.value.ReplaceAll(",",""))
         if(obj.value=="" || obj.value=="0")
            obj.value="1";
          var objSaleOf=obj.parentNode.parentNode.cells[5].children[0];
          if(objSaleOf.value!="" || objSaleOf.value!="0" ) 
          {                 
                objSaleOf.value="0" ;
                fnCalSaleOf(objSaleOf);                
           }
        
    }
    
        function fnDeleteRow($obj)
        {        
               if(confirm('Delete??'))        
               {
                  $objTr=$obj.parent().parent();  
                  $("#<%=hidOrderDelete.ClientID %>").val($objTr.find("td").eq(1).find("span").html()+","+$("#<%=hidOrderDelete.ClientID %>").val())
                  $objTr.remove();                  
                   //Tinh lai tong so luong 
                   fnCalReTotal(); 
                   
                   //Uncheck Payment
                   $("#<%=chkPayment.ClientID %>").removeAttr('checked')  ;
                   $("#<%=txtRepaid.ClientID %>").attr({'value':'0'});
                  fnCalRepaid($("#<%=txtRepaid.ClientID %>"));   
              }
               
        }

       
        function fnCalSaleOf(obj)
        {       
                
               obj.value=FormatMoney(obj.value.ReplaceAll(",",""))   ;         
               var Quantity=obj.parentNode.parentNode.cells[3].children[0].value;
               var Rate=obj.parentNode.parentNode.cells[4].children[1].innerHTML;             
               var DiscountPrice=obj.value.ReplaceAll(",","");

              //neu = 0 lay lai gia goc 
               if(obj.value=="" || obj.value=="0")
               {   
                   DiscountPrice=obj.parentNode.parentNode.cells[4].children[0].innerHTML.ReplaceAll(",","");               
               }          
               var objTotalPrice=obj.parentNode.parentNode.cells[6].children[0];
               var TotalPrice= DiscountPrice*Quantity;           
               if (Rate=="USD")
               { 
                   var USD=document.getElementById("<%= hidUSD.ClientID %>").value ;
                   TotalPrice*= USD;                
                }
          
                objTotalPrice.value=FormatMoney(TotalPrice);    
              //Tinh lai tong so luong 
               fnCalReTotal(); 
               
               //Uncheck Payment
               $("#<%=chkPayment.ClientID %>").removeAttr('checked')  ;
               $("#<%=txtRepaid.ClientID %>").attr({'value':'0'});
                $("#<%=txtPaidMoney.ClientID %>").val("0");
              fnCalRepaid($("#<%=txtRepaid.ClientID %>"));        
        }      
        function fnCalReTotal()
        {          
                var sTotal=0 ; 
                $("#<%= grv_Master.ClientID %>").find('tr').each(function(index){
                    if(index!="0")
                    {                          
                        $objTR=$(this);                   
                        sTotal+=parseInt($objTR.find('td:eq(6)').find('input:eq(0)').attr('value').ReplaceAll(",","")); 
                      
                    }
                });
                 sTotal=FormatMoney(sTotal);
            
                $("#<%=txtRemainPrice.ClientID %>").attr({'value':sTotal});
                $("#<%=txtRepaid.ClientID %>").attr({'value':'0'}); 
                $("#<%=lblTongsotien.ClientID %>").attr({'value':sTotal})  ;          
           
        }
        
//*************************************************        
        //Tinh toan tra truoc
        $(document).ready(function() 
        {
            $("#<%=txtPaidMoney.ClientID %>").keyup(function()
            {
                  fnCalRepaid($(this));
            });
        });
        function fnDisplayTextMoney(obj)
        {
            obj.value=FormatMoney(obj.value.ReplaceAll(",",""))   ; 
        }
        function fnCalRepaid(obj)
        {
            if(obj.attr('value')!="")
            { 
                  $Remainpaid=parseInt(obj.attr('value').ReplaceAll(",",""));
            
                                  
                   //Tong tien-(so tien tra truoc+so tien tra truoc dang nhap)
                  $RemainPrice=$("#<%=lblTongsotien.ClientID %>").attr('value').ReplaceAll(",","")-$Remainpaid;
                   
                  if($RemainPrice<0)
                  {
                    alert('Bạn đã nhập vượt quá số tiền cần thanh toán');
                    obj.focus();
                    obj.attr({'value':'0'});
                    fnCalRepaid(obj);
                    return;
                  }
                  else if($RemainPrice==0)
                     $("#<%=chkPayment.ClientID %>").attr({'checked':'checked'})  ;
                  else
                     $("#<%=chkPayment.ClientID %>").removeAttr('checked')  ;
                     
                  $("#<%=txtRemainPrice.ClientID %>").attr({'value':FormatMoney($RemainPrice)}); 
                  $("#<%=txtRepaid.ClientID %>").attr({'value':FormatMoney($Remainpaid)});
                                     
            }
        }     
        
  //*************************************************
  $(document).ready(function() {
        $("#<%=chkPayment.ClientID %>").click(function()
        {           
                  fnCheckedPayment($(this));
        });
     }); 
   //*************************************************
   function fnCheckedPayment(obj)
   {               
               if(obj.attr('checked')==true)       //Da tra tien day du
              {
                  $("#<%=txtRemainPrice.ClientID %>").attr({'value':'0'});
                  $("#<%=txtRepaid.ClientID %>").attr({'value':$("#<%=lblTongsotien.ClientID %>").attr('value')})  ; 
                  $("#<%=tblPaymoney.ClientID%>").hide();
              }
              else
              {
                   
                   $("#<%=tblPaymoney.ClientID%>").show();                        
                     $("#<%=txtPaidMoney.ClientID %>").val("0");
                   
                  $("#<%=txtRepaid.ClientID %>").attr({'value':"0"})
                  $("#<%=txtRemainPrice.ClientID %>").attr({'value':$("#<%=lblTongsotien.ClientID %>").attr('value')})  ; 
              } 
   }
        
     
        
    
    
    
    </script>

</asp:Content>
